﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MyDB;

namespace MyClassLibrary
{
    public class ClsExamCalender:IMedicare
    {
        MedicareDataClassesDataContext DB = new MyDB.MedicareDataClassesDataContext();
        
        
        public BindingSource GetData(DateTime d)
        {
            BindingSource bs = new BindingSource();
            bs.DataSource = DB.View_ExamCalendars.Where
                (n => n.檢驗日期 == d ||
                (n.檢驗日期.Date <= d.Date.AddDays(n.幾天前提醒) && n.檢驗日期 >= d && n.已提醒 == false) ||
                (n.檢驗日期 == d.AddDays(-1) && n.狀態 == "未完成")).OrderBy(n => n.檢驗日期).ToList();
            return bs;
        }


        public BindingSource GetData()
        {
            BindingSource bs = new BindingSource();
            bs.DataSource = DB.View_ExamCalendars.Where
                (n => n.檢驗日期 == DateTime.Today ||
                (n.檢驗日期.Date <= DateTime.Today.Date.AddDays(n.幾天前提醒) && n.檢驗日期.Date >= DateTime.Today && n.已提醒 == false) ||
                (n.檢驗日期 == DateTime.Today.AddDays(-1) && n.狀態 == "未完成")).ToList();
            return bs;
        }


        public BindingSource Search(string SearchBy, string InputString)
        {
            BindingSource bs = new BindingSource();
            if (SearchBy == "病患姓名")
            {
                var q = from p in DB.View_ExamCalendars
                        where p.病患姓名.Contains(InputString)
                        select p;
                bs.DataSource = q.ToList();
                return bs;
            }
            else
            {
                var q = from p in DB.View_ExamCalendars
                        where p.病歷號碼.ToString().Contains(InputString)
                        select p;
                bs.DataSource = q.ToList();
                return bs;
            }
        }

    }

    public class ClsDrugCalendar
    {

        global::MyDB.MedicareDataClassesDataContext DB = new MyDB.MedicareDataClassesDataContext();

        public BindingSource GetData(DateTime d)
        {
            BindingSource bs = new BindingSource();
            bs.DataSource = DB.View_DrugCalendars.Where
                (n => n.用藥日期 == d ||
                (n.用藥日期.Date <= d.Date.AddDays(n.幾天前提醒) && n.用藥日期 >= d && n.已提醒 == false) ||
                (n.用藥日期 == d.AddDays(-1) && n.狀態 != "完成")).OrderBy(n => n.用藥日期).ToList();
            return bs;
        }

        public BindingSource Search(string SearchBy, string InputString)
        {
            BindingSource bs = new BindingSource();
            if (SearchBy == "病患姓名")
            {
                var q = from p in DB.View_DrugCalendars
                        where p.病患姓名.Contains(InputString)
                        select p;
                bs.DataSource = q.ToList();
                return bs;
            }
            else
            {
                var q = from p in DB.View_DrugCalendars
                        where p.病歷號碼.ToString().Contains(InputString)
                        select p;
                bs.DataSource = q.ToList();
                return bs;
            }
        }

        
    }

}
